package net.huashitong.taskjob.util;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class UtilsTools {
    public final static String msgapiKey= "b3232c91e3336d52ae80b73bb0a1cb91";

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

    //获取当当前年月日
    public static String getCurrentDate() {

        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
        String currentDate = sf.format(date);

        return currentDate;
    }

    //获取当前时间
    public static String getCurrentTime() {
		/*Calendar cal = Calendar.getInstance();
		int hour = cal.get(Calendar.HOUR_OF_DAY);
		int minute = cal.get(Calendar.MINUTE);
		int second = cal.get(Calendar.SECOND);*/
        Date date  = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("HH:mm:ss");
        String time  = sf.format(date);
        return time;
    }

    //获取年份
    public static String getCurrenYear ()
    {
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy");
        String currentYear = sf.format(date);
        return currentYear;
    }

    //获取当前月份
    public static String getCurrentMonth()
    {
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMM");
        String currentMonth = sf.format(date);
        return currentMonth;
    }

    //获取当前日期时间
    public static String getCurrentDateTime(){
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
        String currnetDateTime = sf.format(date);
        return currnetDateTime;
    }

    /**
     * 字符串转日期 YYYYMMDD 格式
     * @param StringDate
     * @return date
     */
    public static Date StringToDate (String StringDate) throws ParseException {
        // = new Date();
        Date date =sdf.parse(StringDate);
        return date;
    }

    /**
     * 日期转字符串 YYYMMDD
     * @param date
     * @return
     */
    public static String DateToString(Date date)
    {

        String Stringdate =sdf.format(date);
        return Stringdate;
    }

    /**
     * 日期加天数的日期
     * @param num
     * @param StringDate
     * @return
     * @throws ParseException
     */
    public static String plusDay(int num ,String StringDate) throws ParseException {
        Date date  = sdf.parse(StringDate);
        System.out.println("现在日期为：" + date);
        Calendar cal  = Calendar.getInstance();
        cal.add(Calendar.DATE,num);
        date=cal.getTime();
        String currentDate = sdf.format(date);
        System.out.println("改变后的日期为：" + currentDate);
        return currentDate;
    }

    //属性复制器 add by xxq
    public static void copyPro(Object obj,Object obj2){
        try {
            BeanInfo soursebean = Introspector.getBeanInfo(obj.getClass(), Object.class);
            PropertyDescriptor[] sourpro =soursebean.getPropertyDescriptors();
            BeanInfo soursebean2 = Introspector.getBeanInfo(obj2.getClass(), Object.class);
            PropertyDescriptor[] sourpro2 =soursebean2.getPropertyDescriptors();

            for (int i = 0; i < sourpro.length; i++) {
                for (int j = 0; j < sourpro2.length; j++) {
                    if(sourpro[i].getName().equals(sourpro2[j].getName())&&sourpro[i].getPropertyType().equals(sourpro2[j].getPropertyType())  ){
                        sourpro2[j].getWriteMethod().invoke(obj2, sourpro[i].getReadMethod().invoke(obj));
                        break;
                    }
                }
            }
        } catch (IntrospectionException e) {
            // TODO Auto-generated catch block

        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
